package com.wangzh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangzh.dao.BookMPDao;
import com.wangzh.entity.Book;
import com.wangzh.service.IBookService;
import org.assertj.core.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl extends ServiceImpl<BookMPDao,Book> implements IBookService {

    @Autowired
    private BookMPDao bookMPDao;
    @Override
    public List<Book> getList(Book book) {


        return bookMPDao.selectList(null);

    }

    @Override
    public IPage<Book> getPage(IPage<Book> page, Book book) {

        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!Strings.isNullOrEmpty(book.getName()),Book::getName,book.getName());
        wrapper.like(!Strings.isNullOrEmpty(book.getType()),Book::getType,book.getType());
        IPage<Book> page1 = bookMPDao.selectPage(page, wrapper);
        return page1;
    }

    @Override
    public boolean update(Book book) {
        int update = bookMPDao.update(null, null);

        return update>0;

    }

    @Override
    public boolean save(Book book) {
        int insert = bookMPDao.insert(book);
        return insert>0;
    }

    @Override
    public boolean delete(Integer id) {
        int i = bookMPDao.deleteById(id);
        return i>0;
    }

    @Override
    public Book getById(Integer id) {
        return bookMPDao.selectById(id);
    }
}
